package com.oocl.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oocl.pojo.GoodsInfo;

/**
 * Servlet implementation class AddShopStoreServlet
 */
public class AddShopStoreServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private List<GoodsInfo> goodsInfoList;
       
    public AddShopStoreServlet() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String userId = request.getParameter("id");
		String bookId = request.getParameter("uuid");
		String bookName = request.getParameter("bookname");
		String username = request.getParameter("username");
		goodsInfoList = (List<GoodsInfo>)request.getSession().getAttribute("goodsInfoList");
		
		if(goodsInfoList == null){
			goodsInfoList = new ArrayList<GoodsInfo>();
			System.out.println("goodsInfoList........... " + goodsInfoList);
		}
		boolean status = false;
		for(int i = 0; i < goodsInfoList.size(); i++){
			if(goodsInfoList.get(i).getBookName().equals(bookName)){
				GoodsInfo temp = new GoodsInfo(userId, bookId, bookName, goodsInfoList.get(i).getCount() + 1);
				goodsInfoList.set(i, temp);
				status = true;
			}
		}
		if(!status){
			GoodsInfo temp = new GoodsInfo(userId, bookId, bookName, 1);
			goodsInfoList.add(temp);
		}
		request.getSession().setAttribute("goodsInfoList", goodsInfoList);
		
		request.getSession().setAttribute("username", username);
		
		request.getRequestDispatcher("store.jsp").forward(request, response);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
